#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    int maximumLengthSubstring(string s) {
        int res = 0;
        int left = 0;
        unordered_map<char, int> hash(26);
        for (int right = 0; right < s.size(); ++right) {
            ++hash[s[right]];
            while (hash[s[right]] > 2) {
                --hash[s[left]];
                ++left;
            }
            res = max(res, right - left + 1);
        }
        return res;
    }
};

int main() {
    Solution s;

    string s1 = "bcbbbcba";
    string s2 = "bbbbb";
    string s3 = "aadaad";

    assert(s.maximumLengthSubstring(s1) == 4);
    assert(s.maximumLengthSubstring(s2) == 2);
    assert(s.maximumLengthSubstring(s3) == 4);

    return 0;
}